 package cn.zjgsu.service.impl;



import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.zjgsu.domain.TbCourse;
import cn.zjgsu.domain.TbCourseStudent;
import cn.zjgsu.domain.TbStudent;
import cn.zjgsu.domain.TbTask;
import cn.zjgsu.domain.TbTeacher;
import cn.zjgsu.domain.TbUser;
import cn.zjgsu.mapping.TbCourseMapper;
import cn.zjgsu.mapping.TbCourseStudentMapper;
import cn.zjgsu.mapping.TbStudentMapper;
import cn.zjgsu.mapping.TbTaskMapper;
import cn.zjgsu.mapping.TbTeacherMapper;
import cn.zjgsu.mapping.TbUserMapper;
import cn.zjgsu.service.ITeacherService;
import cn.zjgsu.utils.SHA1;



@Service
public class TeacherServiceImpl implements ITeacherService{

	@Autowired
	private TbTeacherMapper teacherMapper;
	@Autowired
	private TbTaskMapper taskMapper;
	@Autowired
	private TbUserMapper userMapper;
	@Autowired
	private TbCourseMapper courseMapper;
	@Autowired
	private TbStudentMapper studentMapper;
	@Autowired
	private TbCourseStudentMapper courseStudentMapper;
	@Override
	public TbTeacher getTeacherByFkUser(int fkUser) {
		TbTeacher teacher=teacherMapper.selectByForeignKey((long)fkUser);
		return teacher;
	}
	/**
	 * @author yan
	 *教师更新自己的信息
	 */
	@Override
	public void updateTeacher(TbTeacher teacher) {
		TbTeacher Oteacher = teacherMapper.selectByTeacherNum(teacher.getTeacherNum());
		teacher.setPkTeacher(Oteacher.getPkTeacher());
		teacherMapper.updateByPrimaryKeySelective(teacher);
		
		
	}
	/**
	 * @author yan
	 *教师修改个人密码
	 */
	@Override
	public void changeTeacherPassword(int pkTeacher, String changePassword) {
	TbTeacher teacher =teacherMapper.selectByPrimaryKey((long)pkTeacher);
	int pkUser=teacher.getFkUser();
	TbUser user= userMapper.selectByPrimaryKey((long)pkUser);
		if(teacher.getFkUser()!=null)
		{
			teacher.setTeacherPwd(SHA1.encrypt(changePassword));
		    user.setPassword(SHA1.encrypt(changePassword));
		    userMapper.updateByPrimaryKeySelective(user);
		 teacherMapper.updateByPrimaryKeySelective(teacher);
		}
		
	}

	
	
	/**
	 * @author yan
	 *获取教师所有课程的年份
	 */
	@Override
	public List<String> getYear(int fkTeacher) {
		
		List<String> list=courseMapper.getYear(fkTeacher);
		//这一部分是数据库获取的年份ArrayList去重
			 for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )   {   
				    for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )   {   
				      if  (list.get(j).equals(list.get(i)))   {   
				        list.remove(j);   
				      }   
				    }   
				  }   

		return list;
	}
	@Override
	public List<String> findCourseByYear(Integer fkTeacher, String year) {
		
		
		
		List<String> list=courseMapper.findCourseByYear(fkTeacher, year);
		
		if(list==null)
		{
			
			list.add("无");
			
		}
		
		return list;
	}
	
	
	
	/**
	 * @author yan
	 *单个学生导入课程
	 */
	@Override
	public void linkStudentCourse(String year, String courseName, int fkStudent, int fkTeacher) {
		
	   int fkCourse=(int)courseMapper.findPkCourse(fkTeacher,year,courseName);
	   TbCourseStudent courseStudent =new TbCourseStudent();
	   courseStudent.setFkCourse(fkCourse);
	   courseStudent.setFkStudent(fkStudent);
	  courseStudentMapper.insertSelective(courseStudent);
	}
	
	
	/**
	 * @author yan
	 *批量学生导入课程
	 */
	@Override
	public void linkStudentCourseExcel(String year, String courseName, int fkTeacher, InputStream in, String fileName)
			throws Exception {
		 int fkCourse=(int)courseMapper.findPkCourse(fkTeacher,year,courseName);
		List<TbStudent> list;
		if(fileName.contains("xlsx")){
			list=imp_07(in);
		}else{
			list=imp(in);
		}
		
		for(TbStudent student:list){
			
			 TbCourseStudent courseStudent =new TbCourseStudent();
			 String studentNum=student.getStudentNum();
			 //通过num找到数据库学生的id
			 TbStudent student2=studentMapper.selectByStudentNum(studentNum);
			   courseStudent.setFkCourse(fkCourse);
			   courseStudent.setFkStudent(student2.getPkStudent());
			  courseStudentMapper.insertSelective(courseStudent);
				
			
		}
		
	}

	/**
	 * @author yan
	 *Excel文件读取xsl
	 */
	
	private List<TbStudent> imp_07(InputStream in) throws Exception{
		List<TbStudent> list;
		try {
			list=new ArrayList<TbStudent>();
			XSSFWorkbook hwb = new XSSFWorkbook(in);
			
			
			// 循环工作表sheet
			for (int num = 0; num < hwb.getNumberOfSheets(); num++) {
				
				XSSFSheet hssfSheet = hwb.getSheetAt(num);
				if (hssfSheet == null) {
					continue;
				}
				// 循环行Row,在excel表中直接就是第二行就上工号名字了
				for (int row = 1; row <= hssfSheet.getLastRowNum(); row++) {
				
					TbStudent student =new TbStudent();
					XSSFRow hssfRow = hssfSheet.getRow(row);
					if (hssfRow == null) {
						continue;
					}
					// 循环列Cell
					// for (int cell = 0; cell <= 4; cell++) {
					XSSFCell number = hssfRow.getCell(0);
					if (number == null) {
						continue;
					}
					System.out.print(sub(number.getRawValue()));
					student.setStudentNum(sub(number.getRawValue()));

					list.add(student);
				}
			}
		} catch (IOException e) {
			throw new Exception("读取excel有误！");
		}
		
		return list;
	}
	
	
	
	/**
	 * @author yan
	 *Excel读取xxsl
	 *
	 */

	public List<TbStudent> imp(InputStream in) throws Exception {
		List<TbStudent> list;
		try {
			list=new ArrayList<TbStudent>();
			HSSFWorkbook hwb = new HSSFWorkbook(in);
			
			
			// 循环工作表sheet
			for (int num = 0; num < hwb.getNumberOfSheets(); num++) {
				
				HSSFSheet hssfSheet = hwb.getSheetAt(num);
				if (hssfSheet == null) {
					continue;
				}
				// 循环行Row
				for (int row = 1; row <= hssfSheet.getLastRowNum(); row++) {
					TbStudent student =new TbStudent();
					HSSFRow hssfRow = hssfSheet.getRow(row);
					if (hssfRow == null) {
						continue;
					}
					// 循环列Cell
					// for (int cell = 0; cell <= 4; cell++) {
					HSSFCell number = hssfRow.getCell(0);
					if (number == null) {
						continue;
					}
					student.setStudentNum(sub(getValue(number)));

					list.add(student);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new Exception("读取excel有误！");
		}
		
		return list;
	}
			
			public String sub(Object o) {
				String s = o + "";
				if (s.contains(".")) {
					s = s.substring(0, s.indexOf("."));
				}
				return s;
			}

			/*
			 * 得到Excel表中的值
			 * 
			 * @param hssfCell Excel中的每一个格子
			 * 
			 * @return Excel中每一个格子中的值
			 */
		
			private String getValue(HSSFCell hssfCell) {
				if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
					// 返回布尔类型的值
					return String.valueOf(hssfCell.getBooleanCellValue());
				} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
					// 返回数值类型的值
					return String.valueOf(hssfCell.getNumericCellValue());
				} else {
					// 返回字符串类型的值
					return String.valueOf(hssfCell.getStringCellValue());
				}
			}
			
			
			@Override
			public List<TbCourse> findCourseRecordByYear(Integer fkTeacher, String year) {
				
				
				List<TbCourse> list=courseMapper.findCourseRecordByYear(fkTeacher, year);
				
				
				return list;
			}
			
			/*
			 * 得到教师个人所有的task
			 * 
			 */
		
			@Override
			public int getTaskCount(int fkUser) {
				int count=(int)taskMapper.getCount(fkUser);
				return count;
			}
			
	
			@Override
			public List<TbTask> selectByPage(int startPos, int pageSize, int fkUser) {
				List<TbTask> list=taskMapper.selectByPage(startPos, pageSize,fkUser);
				return list;
			}}
